<%@ page import="custom.*, java.util.*" %>
<html>
<%	
	int degreeLocId = Integer.parseInt( request.getParameter("degreeLoc"));
	LinkedHashMap<String, Object> degree = new LinkedHashMap<String, Object>();
	// store the degree location id
	degree.put("location", degreeLocId);
	Vector<LinkedHashMap<String, Object>> degrees = (Vector<LinkedHashMap<String, Object>>)session.getAttribute("degrees");
	// create a new "degrees" vector object, if it has not been created already, for the degree(s) information of the applicant 
	if(degrees == null)
		degrees = new Vector<LinkedHashMap<String, Object>>();
	// set session attribute for degree location
	degrees.add(degree);
	session.setAttribute("degrees", degrees);
	 // get list of universities from chosen location

    Vector uniTuple = support.getUniversities();
    Vector tuple = (Vector) uniTuple.get(degreeLocId);
    Vector uni = (Vector)tuple.get(1);
    String degreeLoc=(String)((Vector)uniTuple.get(degreeLocId)).get(0);
%>
<head>
<title>Provide degrees- Choose university</title>
<script type="text/javascript">
    function validate() {
    	var degreeLoc = document.getElementById("degreeLoc").value
        var customUnivName = document.getElementById("customUnivName").value;
        // alert(degreeLoc +", " + customUnivName);
    	var xmlHttp = new XMLHttpRequest();
    	var responseHandler = function() {
    	    if(xmlHttp.readyState == 4) {
            }
    	}
    	xmlHttp.onreadystatechange = responseHandler;
    	xmlHttp.open("GET","validateUniv.jsp?degreeLoc="+degreeLoc+"&univ="+customUnivName,false); // make synchronous call
    	xmlHttp.send(null);
    	if(xmlHttp.responseText==0) {
    		alert("Your inputted university already appears in the page.");
    		return false;
    	}
    	return true;
    }
</script>
</head>
<body>
<input id="degreeLoc" type="hidden" value="<%=degreeLoc%>"/>
<h3>Please choose your university: </h3> <p>
    <table>
    <%
    for(int i=0;i<uni.size(); i++) {
    	if(i%3==0) {
    		if(i!=0) {
    			%></tr><%
    		}
    		%><tr><%
    	}
    	%><td><a href="degreeDisc.jsp?degreeUniName=<%= ((Vector)uni.get(i)).get(0) %>&degreeUniID=<%= ((Vector)uni.get(i)).get(1) %>"><%= ((Vector)uni.get(i)).get(0) %></a></td>
    <%}
	%>
	</tr>
	</table>
 <p>
If you cannot find your university from the list above, please enter the name here: <p>
<form method="GET" action="degreeDisc.jsp" onsubmit="return validate()">
<input id="customUnivName" type="text" size="20" name="degreeUniName" />
<input type="submit" value="Submit"/>
</form>
</body>
</html>
